# Optimal plot
# ggplot----

# Barplot
ggplot(mpg, aes(class, displ)) + 
  geom_bar(stat = "identity", fill = "steelblue")
ggplot(mpg, aes(class, displ)) + 
  geom_bar(stat = "identity", fill = "steelblue") +
  coord_flip()

# Sort
data <- mtcars[0:8, ]
ggplot(data, aes(row.names(data), qsec)) + 
  geom_bar(stat = "identity", fill = "steelblue")
ggplot(data, aes(reorder(row.names(data), -qsec), qsec)) + 
  geom_bar(stat = "identity", fill = "steelblue") # descending data by 'qsec'

ggplot(data, aes(reorder(row.names(data), -qsec), qsec)) + 
  geom_bar(stat = "identity", fill = "steelblue") +
  coord_flip()

ggplot(data, aes(reorder(row.names(data), -qsec), qsec)) + 
  geom_bar(stat = "identity", fill = "steelblue") +
  theme(axis.text.x = element_text(angle = 90, colour = "black")) + 
  labs(x = "Cars", y = "Q seconds")

ggplot(data, aes(reorder(row.names(data), -qsec), qsec)) + 
  geom_bar(stat = "identity", fill = "steelblue") +
  theme(axis.text.x = element_text(angle = 90, size = 10, colour = "black"),
        axis.text.y = element_text(size = 10, colour = "black")) + 
  labs(x = "Cars", y = "Q seconds") +
  theme(text = element_text(family = "arial", size = 20)) + 
  ggtitle("BarPlot")

# Background
ggplot(data, aes(reorder(row.names(data), -qsec), qsec)) + 
  geom_bar(stat = "identity", fill = "steelblue") +
  theme(axis.text.x = element_text(angle = 90, size = 10, colour = "black"),
        axis.text.y = element_text(size = 10, colour = "black")) + 
  labs(x = "Cars", y = "Q seconds") +
  theme(text = element_text(family = "arial", size = 20)) + 
  ggtitle("BarPlot") + 
  theme(panel.grid = element_blank()) + 
  theme(panel.background = element_blank())

# Width of bar
ggplot(data, aes(reorder(row.names(data), -qsec), qsec)) + 
  geom_bar(stat = "identity", fill = "steelblue", width = 0.5)

# Add label for each column
ggplot(data, aes(reorder(row.names(data), -qsec), qsec)) + 
  geom_bar(stat = "identity", fill = "steelblue", width = 0.5) +
  geom_text(aes(label = qsec, vjust = -0.8, just = 0.5))

# Full code
ggplot(data, aes(reorder(row.names(data), -qsec), qsec)) +  # sorting by X axis
  geom_bar(stat = "identity", fill = "SandyBrown", width = 0.8) +  # fill color, bar width
  labs(x = "Cars", y = "Q seconds") +   # X, Y axis labels
  ggtitle("An Indrouction of BarPlot ") + # Figure title
  geom_text(aes(label = qsec, vjust = -0.5, hjust = 0.5)) +  # Add data label
  theme(axis.text.x = element_text(angle = 90,size = 12,colour = "DarkGrey"),
    # X axis label (rotation, size, color)
    axis.text.y = element_text(size = 12, colour = "DarkGrey"),
    # Y axis label (size, color)
    axis.ticks.length = unit(0.5, 'cm'),
    # Axis ticks length
    text = element_text(family = "arial", size = 20, colour = "DimGrey"),
    # Text in figure
    panel.grid = element_blank(), panel.background = element_blank())  # Clear backgroud and grid

# theme----
library(ggthemes)
library(grid)

data <- data.frame(
    conpany = c("Apple", "Google", "Facebook", "Amozon", "Tencent"),
    Revenue = c(5000, 3500, 2300, 2100, 3100))

p <- ggplot(data, aes(reorder(conpany, -Revenue), Revenue)) + 
  geom_bar(stat = "identity") + 
  labs(x = "Company", y = "The Revenue of 2015 ($)") + 
  ggtitle("The financial performance of five giant")

p + theme_bw()

p + theme_wsj()

# Theme and color theme
ggplot(data, aes(reorder(conpany, -Revenue), Revenue, fill = "steelblue")) + 
  geom_bar(stat = "identity") + 
  labs(x = "Company", y = "The Revenue of 2015 ($)") + 
  ggtitle("The financial performance of five giant") + 
  theme_wsj() + scale_fill_wsj()  # Theme and color theme

# optimal
ggplot(data, aes(reorder(conpany, -Revenue), Revenue, fill = "steelblue")) + 
  geom_bar(stat = "identity") + 
  labs(x = "Company", y = "The Revenue of 2015 ($)") + 
  ggtitle("The financial performance of five giant") + 
  theme_wsj() + scale_fill_wsj() + 
  geom_text(aes(label = Revenue, vjust = -0.5, hjust = 0.5)) +
  theme(legend.position = 'none', axis.ticks.length = unit(0.5, 'cm'))

ggplot(data, aes(reorder(conpany, -Revenue), Revenue, fill = "steelbule")) +
  geom_bar(stat = "identity") +
  labs(x = "Company", y = "The Revenue of 2015($)") +
  ggtitle("The Financial Performance of five giant") +
  theme_wsj() +
  scale_fill_manual(values = c("#FB882C", "#5B88A0")) +
  geom_text(aes(label = Revenue, vjust = -0.5,hjust = 0.5)) +
  theme(legend.position = 'none', axis.ticks.length = unit(0.5, 'cm'))

ggplot(data, aes(reorder(conpany, -Revenue), Revenue, fill = "steelbule")) +
  geom_bar(stat = "identity") +
  labs(x = "Company", y = "The Revenue of 2015($)") +
  ggtitle("The Financial Performance of five giant") +
  theme_economist(base_size = 14) +
  scale_fill_economist() +
  geom_text(aes(label = Revenue, vjust = -0.5, hjust = 0.5)) +
  theme(legend.position = 'none', axis.ticks.length = unit(0.5, 'cm'))

ggplot(data, aes(reorder(conpany, -Revenue), Revenue, fill = "steelbule")) +
  geom_bar(stat = "identity", width = 0.65) +
  labs(x = "Company", y = "The Revenue of 2015($)") +
  ggtitle("The Financial Performance of five giant") +
  theme_economist(base_size = 14) +
  scale_fill_economist() +
  geom_text(aes(label = Revenue, vjust = -0.5, hjust = 0.5)) +
  theme(legend.position = 'none', axis.ticks.length = unit(0.5, 'cm'))
